#include "eveCommon.h"

#include "eveGuiElement.h"
#include "eveGuiControl.h"
#include "eveClipImage.h"

//-------------------------------------------------------------------------------------
CGuiBlendColor::CGuiBlendColor()
{
	memset(m_blendColor,0,sizeof(m_blendColor));
}

CGuiBlendColor::~CGuiBlendColor()
{
}

void CGuiBlendColor::Blend(GUI_CONTROL_STATE state,Real ftimeDelta,D3DXCOLOR& curcolor)
{
	D3DXCOLOR dstcolor = m_blendColor[state];
	D3DXColorLerp(&curcolor,&curcolor,&dstcolor,1.0f - powf(0.007f,30 * ftimeDelta));
}


//-------------------------------------------------------------------------------------
CGuiElement::CGuiElement()
: m_pClipBmp(NULL)	
{
}

CGuiElement::~CGuiElement()
{
}

void CGuiElement::Render(const stRectI& rc,DWORD textblendcolor)
{
	if( m_pClipBmp )this->m_pClipBmp->Render(rc,textblendcolor);
}

void CGuiElement::Render(const stRectI& rc,const String& str,size_t font,DWORD fontblendcolor,uint32 align)
{
	//if( this->m_pClipBmp )
	//	this->m_pClipBmp->Render(rc,
	eve::FontManager::GetSingleton()->Render(str,rc,font,fontblendcolor,align);

}
void CGuiElement::SetFontBlendColor(GUI_CONTROL_STATE state,DWORD color)
{
	m_FontBlend.SetBlendColor(state,color);
}

void CGuiElement::SetTextureBlendColor(GUI_CONTROL_STATE state,DWORD color)
{
	m_TextureBlend.SetBlendColor(state,color);
}




//-------------------------------------------------------------------------------------
CGuiElementInstance::CGuiElementInstance(CGuiElement* element)
: m_pElement(element)
, m_CurTextureColor(0xffffffff)
, m_CurFontColor(0xffffffff)
{

}

CGuiElementInstance::~CGuiElementInstance()
{
}

void CGuiElementInstance::Render(CGuiControl* ctrl,const stRectI& rc,Real timeDelta)
{
	m_pElement->m_TextureBlend.Blend(ctrl->GetState(),timeDelta,this->m_CurTextureColor);
	m_pElement->Render(rc,m_CurTextureColor);
}

void CGuiElementInstance::Render(CGuiControl *ctrl, const stRectI &rc, const String &str,size_t font,DWORD dwClr,uint32 align,Real timeDelta)
{
	m_pElement->m_FontBlend.Blend(ctrl->GetState(),timeDelta,this->m_CurFontColor);
	m_pElement->Render(rc,str,font,m_CurFontColor,align);
}





//-------------------------------------------------------------------------------------
CGuiElementHolder::CGuiElementHolder()
: m_pElementHolders(NULL)
{

}

CGuiElementHolder::~CGuiElementHolder()
{
	for( vector<CGuiElement*>::iterator iter = m_elements.begin(); 
		 iter != m_elements.end(); ++iter )
	{
		SAFE_DELETE((*iter));
	}
	m_elements.clear();
	SAFE_DELETE(m_pElementHolders);
}

void CGuiElementHolder::AddElement(const String&name,CGuiElement* element)
{
	this->m_elements.push_back(element);
}

void CGuiElementHolder::AddElementHolder(const String &name, CGuiElementHolder *hld)
{
	_Allocate();
	this->m_pElementHolders->insert(std::make_pair(name,hld));
}

void CGuiElementHolder::_Allocate()
{
	if( !m_pElementHolders )
		m_pElementHolders = new map<String,CGuiElementHolder*>();
}

CGuiElementHolder* CGuiElementHolder::GetElementHolder(const String& name)
{
	if( m_pElementHolders )
	{
		map<String,CGuiElementHolder*>::iterator iter = m_pElementHolders->find(name);
		if( iter != m_pElementHolders->end() )
		{
			return (*iter).second;
		}
	}
	return NULL;
}
CGuiElement* CGuiElementHolder::GetElementByIndex(size_t index)
{
	//map<String,CGuiElement*>::iterator iter = m_elements.find(name);
	//if( iter != m_elements.end() )
	//{
	//	return (*iter).second;
	//}
	if( index < m_elements.size() )
		return m_elements[index];
	return NULL;
}
//CGuiElement* CGuiElementHolder::GetElementByName(const String& name)
//{
//	map<String,CGuiElement*>::iterator iter = m_elements.find(name);
//	if( iter != m_elements.end() )
//	{
//		return (*iter).second;
//	}
//	return NULL;
//}
//-------------------------------------------------------------------------------------
CGuiElementInstanceHolder::CGuiElementInstanceHolder(const CGuiElementHolder* eleholder)
{
	if( eleholder )
	{
		for( vector<CGuiElement*>::const_iterator iter = eleholder->m_elements.begin(); 
			 iter != eleholder->m_elements.end(); ++iter )
		{
			CGuiElement* element = (*iter);
			CGuiElementInstance* instance = new CGuiElementInstance(element);
			AddElement(instance);
		}
	}
}

CGuiElementInstanceHolder::~CGuiElementInstanceHolder()
{
	for( vector<CGuiElementInstance*>::iterator iter = m_elementinstances.begin(); 
		 iter != m_elementinstances.end(); ++iter )
	{
		SAFE_DELETE((*iter));
	}
	m_elementinstances.clear();
}
void CGuiElementInstanceHolder::AddElement(CGuiElementInstance *element)
{
	//map<String,CGuiElementInstance*>::iterator iter = m_elements.find(name);
	//if( iter != m_elements.end() )
	//{
	//	delete (*iter).second;
	//	(*iter).second = element;
	//}
	//m_elements.insert(std::make_pair(name,element));
	m_elementinstances.push_back(element);
}

void CGuiControlRender::Render(Real timeDelta)
{
	
	for( vector<CGuiElementInstance*>::iterator iter = m_elementinstances.begin(); 
	 iter != m_elementinstances.end(); ++iter )
	{
		CGuiElementInstance* instance = (*iter);
		if( instance )
		{
			//if( instance->m_pElement->m_pClipBmp )
			instance->Render(&m_rControl,
			m_rControl.GetClientRect(),
			timeDelta);

		}
	}
}
